<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-admin' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>系列列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="id" width="100" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="180" align="center"></el-table-column>
      <el-table-column prop="brandId" label="品牌Id" width="150" align="center"></el-table-column>
      <el-table-column prop="sort" label="排序序号" width="150" align="center"></el-table-column>
      <el-table-column prop="keywords" label="关键字" width="200" align="center"></el-table-column>
      <el-table-column prop="enable" label="是否启用" width="150" align="center"></el-table-column>

<!--      <el-table-column label="子级类别" width="100" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="showSubCategories(scope.row)" :disabled="scope.row.isParent == 0">查看
          </el-button>
        </template>
      </el-table-column>-->
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
                     @click="handleEdit(scope.row.id)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                     :disabled="scope.row.isParent == 1"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 15px 0; text-align: right;" v-if="parentId != 0">
      <el-button size="mini" @click="backToRootList()">返回</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    }
  },
  methods: {
    /*showSubCategories(currentCategory) {
      this.parentId = currentCategory.id;
      this.loadCategoryList();
    },
    backToRootList() {
      this.parentId = 0;
      this.loadCategoryList();
    },*/
    openDeleteConfirm(series) {
      this.$confirm('即将永久删除【' + series.name + '】类别, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(series.id);
      }).catch(() => {
      });
    },
    handleEdit(id) {
      console.log('准备【编辑类别】，数据id为【' + id + '】……');
      this.$alert('当前功能【编辑类别】尚未实现！', '操作无法完成', {
        confirmButtonText: '确定', type: 'warn', showClose: false
      });
    },
    handleDelete(id) {
      console.log('准备【删除系列】，数据id为【' + id + '】……');
      let url = 'http://localhost:9081/series/' + id + '/delete';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url)
          .then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              console.log('删除成功！');
              this.$message({
                message: '删除成功！',
                type: 'success'
              });
            } else if (responseBody.state == 40400) {
              console.log(responseBody.message);
              this.$message.error(responseBody.message);
            } else {
              console.log(responseBody.message);
              this.$message.error(responseBody.message);
              return;
            }
            this.loadSeriesList();
          })
    },
    loadSeriesList() {
      console.log('准备从服务器端加载【系列列表】……');
      let url = 'http://localhost:9081/series';
      console.log('请求路径：' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url)
          .then((response) => {
            let responseBody = response.data;
            console.log('接收到服务器端响应的结果：');
            console.log(responseBody);
            this.tableData = responseBody.data;
          });
    }
  },
  mounted() {
    console.log('mounted');
    this.loadSeriesList();
  }
}
</script>

<style scoped>

</style>